package com.fyw.xmcamera;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lib.funsdk.support.FunError;
import com.lib.funsdk.support.FunSupport;
import com.lib.funsdk.support.OnFunDeviceListener;
import com.lib.funsdk.support.OnFunDeviceOptListener;
import com.lib.funsdk.support.models.FunDevType;
import com.lib.funsdk.support.models.FunDevice;
import com.lib.funsdk.support.models.FunLoginType;
import com.lib.sdk.struct.H264_DVR_FILE_DATA;

/**
 * 添加设备页面
 * Created by FYW on 2017/10/19.
 */

public class AddDeviceActivity extends BaseActivity implements OnFunDeviceListener, OnFunDeviceOptListener {
    private FunDevice mFunDevice = null;

    private EditText addDeviceSn;
    private TextView addDeviceBtn;

    @Override
    protected void handleToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.setTitle("添加设备");
        toolbarHelper.setFinish().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initParam(Bundle param) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_add_device;
    }

    @Override
    public void initView(View view) {
        addDeviceSn = view.findViewById(R.id.addDeviceSn);
        addDeviceBtn = view.findViewById(R.id.addDeviceBtn);
        // 设置登录方式为互联网方式
        FunSupport.getInstance().setLoginType(FunLoginType.LOGIN_BY_INTENTT);

        // 监听设备类事件
        FunSupport.getInstance().registerOnFunDeviceListener(this);

        // 设备操作类事件(登录是否成功需要)
        FunSupport.getInstance().registerOnFunDeviceOptListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        addDeviceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestDeviceLogin();
            }
        });
    }

    @Override
    public void onDeviceListChanged() {

    }

    @Override
    public void onDeviceStatusChanged(FunDevice funDevice) {

    }

    @Override
    public void onDeviceAddedSuccess() {
        finish();
    }

    @Override
    public void onDeviceAddedFailed(Integer errCode) {
        showToast(FunError.getErrorStr(errCode));
    }

    @Override
    public void onDeviceRemovedSuccess() {

    }

    @Override
    public void onDeviceRemovedFailed(Integer errCode) {

    }

    @Override
    public void onAPDeviceListChanged() {

    }

    @Override
    public void onLanDeviceListChanged() {

    }

    @Override
    public void onDeviceLoginSuccess(FunDevice funDevice) {
        if (null != mFunDevice
                && null != funDevice
                && mFunDevice.getId() == funDevice.getId()) {
            requestDeviceAdd();
        }
    }

    @Override
    public void onDeviceLoginFailed(FunDevice funDevice, Integer errCode) {
        showToast(FunError.getErrorStr(errCode));
    }

    @Override
    public void onDeviceGetConfigSuccess(FunDevice funDevice, String configName, int nSeq) {

    }

    @Override
    public void onDeviceGetConfigFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceSetConfigSuccess(FunDevice funDevice, String configName) {

    }

    @Override
    public void onDeviceSetConfigFailed(FunDevice funDevice, String configName, Integer errCode) {

    }

    @Override
    public void onDeviceChangeInfoSuccess(FunDevice funDevice) {

    }

    @Override
    public void onDeviceChangeInfoFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceOptionSuccess(FunDevice funDevice, String option) {

    }

    @Override
    public void onDeviceOptionFailed(FunDevice funDevice, String option, Integer errCode) {

    }

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice) {

    }

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice, H264_DVR_FILE_DATA[] datas) {

    }

    @Override
    public void onDeviceFileListGetFailed(FunDevice funDevice) {

    }

    private void requestDeviceLogin() {
        String devSN = addDeviceSn.getText().toString().trim();

        if (devSN.length() == 0) {
            showToast("序列号不正确");
            return;
        }

        mFunDevice = null;

        if (null == mFunDevice) {
            // 虚拟一个设备, 只需要序列号和设备类型即可添加
            mFunDevice = new FunDevice();
            mFunDevice.devSn = devSN;
            mFunDevice.devName = devSN;
            mFunDevice.devType = FunDevType.EE_DEV_NORMAL_MONITOR;
            mFunDevice.loginName = "admin";
            mFunDevice.loginPsw = "";
        }

        // 添加设备之前都必须先登录一下,以防设备密码错误,也是校验其合法性
        FunSupport.getInstance().requestDeviceLogin(mFunDevice);
    }

    private void requestDeviceAdd() {
        if (null != mFunDevice) {
            FunSupport.getInstance().requestDeviceAdd(mFunDevice);
        }
    }
}
